package com.gs.m_goods_detail

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.gs.common.BaseCompatActivity
import com.gs.common.global.GlobalVariables
import com.gs.common.util.ActionBarUtil
import com.gs.common.util.ToastUtil
import com.gs.m_goods_detail.databinding.GoodsDetailLayoutBinding
import com.gs.common.data.Good
import com.gs.common.util.DialogUtil


class GoodsDetailActivity : BaseCompatActivity(),
    GoodsDetailPersistent.OnDoGetUserInfoByUserIdStateCallback {

    private lateinit var binding: GoodsDetailLayoutBinding
    private val goodsDetailPersistent by lazy { GoodsDetailPersistent.getInstance(this) }
    private var mGoodPosition: Int = -1
    private lateinit var good: Good

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = GoodsDetailLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 设置自定义 action bar
        ActionBarUtil.setCustomActionBar(this, R.layout.goods_detail_actionbar_layout)

        // 设置返回按钮
        val backBtn = findViewById<ImageView>(R.id.back_btn)
        backBtn.setOnClickListener {
            finish()
        }

        mGoodPosition = intent.getIntExtra("position", -1)
        if (mGoodPosition != -1) {
            good = GlobalVariables.goodsList?.get(mGoodPosition)!!
            val url = good.picture_path
            val price = good.price
            val detailTitle = good.detail_title
            val count = good.count
            val userId = good.user_id
            Log.d("userId", "${good.user_id}")
            showBusinessInfo(userId)
            Glide.with(this).load(url).into(binding.goodImg)
            binding.price.text = price.toString()
            binding.detailTitleTv.text = detailTitle
//            binding.goodCountTv.text = "库存量：$count"
        } else {
            ToastUtil.showShortToast(this, "${GoodsDetailActivity::class}资源加载失败！")
        }
    }

    override fun onResume() {
        super.onResume()
        binding.addGoodToCartBtn.setOnClickListener {
            val loginUser = GlobalVariables.loginUser
            if (mGoodPosition == -1 || loginUser == null) return@setOnClickListener
            val username = loginUser.userName
            if (GlobalVariables.businessInfo?.userName == loginUser.userName) {
                val alertDialog = DialogUtil.createCommonAlertDialog(
                    this@GoodsDetailActivity,
                    "把自家商品加入购物车",
                    ""
                ) {
                    if (it == DialogUtil.CONFIRM_TO_DO) {
                        addGoodToCart(username)
                    }
                }
                alertDialog.show()
            } else {
                addGoodToCart(username)
            }
        }
        // 设置销售量
        getSoldCount()
    }

    private fun addGoodToCart(username: String) {
        goodsDetailPersistent.addGoodToCart(
            good.id,
            username,
            object : GoodsDetailPersistent.OnAddGoodToCartStateCallback {
                override fun onAddGoodToCartSuccess() {
                    ToastUtil.showShortToast(this@GoodsDetailActivity, "加入成功")
                }

                override fun onAddGoodToCartFailed() {
                    ToastUtil.showShortToast(this@GoodsDetailActivity, "加入失败")
                }

            })
    }

    private fun getSoldCount() {
        goodsDetailPersistent.getSoldCount(
            good.id,
            object : GoodsDetailPersistent.OnGetSoldCountStateCallback {
                @SuppressLint("SetTextI18n")
                override fun onGetSoldCountSuccess() {
                    binding.goodSoldCountTv.text =
                        "已售出：${GlobalVariables.goodSoldCount} (单)"
                }

                override fun onGetSoldCountFailed() {
                }

            })
    }

    private fun showBusinessInfo(userId: Int) {
        goodsDetailPersistent.getUserInfoByUserId(userId, this)
    }

    @SuppressLint("SetTextI18n")
    override fun onGetUserInfoByUserIdSuccess() {
        binding.businessTelTv.text = "商家手机号：" + GlobalVariables.businessInfo?.phoneNumber
        val businessAddress =
            if (GlobalVariables.businessInfo == null || GlobalVariables.businessInfo?.address == "null") {
                "暂无地址"
            } else {
                GlobalVariables.businessInfo?.address
            }
        binding.businessLocationTv.text = "商家地址：$businessAddress"
    }

    override fun onGetUserInfoByUserIdFailed() {
        ToastUtil.showShortToast(this, "商家信息查询失败")
    }
}